package com.shuqi.model;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Context;

import com.shuqi.common.MyTask;
import com.shuqi.common.Urls;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.Util;
import com.shuqi.database.model.UserInfo;
import com.shuqi.interfaces.EventTypeConstants;
import com.shuqi.model.bean.AccountInfo;
import com.shuqi.model.net.HttpRequest;
import com.shuqi.model.net.UrlManager;
import com.shuqi.model.net.listener.HttpRequestListener;
import com.shuqi.model.parse.parser.AccountParser;

/**
 * 获取在线用户身份
 * 
 * @author yanghuawei
 */
public class CheckUserStateOnlineModel implements HttpRequestListener {
    private final String logTag = "CheckUserStateOnlineModel";
    private HttpRequest httpRequest;
    private OnCheckUserStateOnlineListener listener;
    private Context context;

    public CheckUserStateOnlineModel(Context context, OnCheckUserStateOnlineListener listener) {
        this.listener = listener;
        this.context = context.getApplicationContext();
    }

    public void checkOnline() {
        httpRequest =
                new HttpRequest(context, UrlManager.COLUMN_DOMAIN_ACCOUNT, getRequestUrl(), setParams(),
                        this);
        httpRequest.setDataParseAdapter(new AccountParser());
        MyTask.runInBackground(httpRequest, true);
    }

    @Override
    public void action(int actionCode, Object object) {
        switch (actionCode) {
            case EventTypeConstants.EVENT_SUCCESS:// 读取数据 写处理逻辑
                AccountInfo accountInfo = (AccountInfo) object;
                onCheckFinish(accountInfo);
                Log4an.i(logTag, accountInfo.toString());
                break;
            default:
                listener.OnFinish(UserInfo.TYPE_CHECKRESULT_FAIL_RECHECK, null);
                break;
        }
    }

    private void onCheckFinish(AccountInfo accountInfo) {
        if (accountInfo != null && "200".equals(accountInfo.getServerState())) {
            // 1会员，2准会员
            if ("1".equals(accountInfo.getGrade())) { // 会员
                if ("1".equals(accountInfo.getValidate())) { // 会员身份验证通过
                    Log4an.e(logTag, "会员，验证通过：" + accountInfo.getValidate());
                    listener.OnFinish(UserInfo.TYPE_CHECKRESULT_VIPUSER_PASSED, accountInfo);
                } else { // 会员验证失败
                    Log4an.e(logTag, "会员，验证失败，需要登录：" + accountInfo.getValidate());
                    listener.OnFinish(UserInfo.TYPE_CHECKRESULT_FAIL_RECHECK, accountInfo);
                }
            } else if ("2".equals(accountInfo.getGrade())) { // 准会员
                String code = accountInfo.getUniqueCode();
                if ("200".equals(code)) { // 会员验证通过
                    Log4an.e(logTag, "会员，验证通过" + accountInfo.getUniqueCode());
                    listener.OnFinish(UserInfo.TYPE_CHECKRESULT_VIPUSER_PASSED, accountInfo);
                } else if ("201".equals(code)) { // 准会员验证通过，绑定sn
                    Log4an.e(logTag, "准会员，验证通过，绑定sn：" + accountInfo.getUniqueCode());
                    listener.OnFinish(UserInfo.TYPE_CHECKRESULT_USER_PASSED, accountInfo);
                } else if ("202".equals(code)) { // 准会员验证通过，新分配userId
                    Log4an.e(logTag, "准会员验证通过，分配新的userid：" + accountInfo.getUniqueCode());
                    listener.OnFinish(UserInfo.TYPE_CHECKRESULT_USER_PASSED_NEWUSER, accountInfo);
                } else if ("203".equals(code)) { // 准会员验证失败，下次需要重新验证
                    Log4an.e(logTag, "准会员验证，服务器绑定sn失败，下次需要重新验证" + accountInfo.getUniqueCode());
                    listener.OnFinish(UserInfo.TYPE_CHECKRESULT_FAIL_RECHECK, accountInfo);
                } else { // 准会员验证失败，降级为800万身份
                    listener.OnFinish(UserInfo.TYPE_CHECKRESULT_FAIL, accountInfo);
                    Log4an.e(logTag, "验证失败：" + code + "，降级为800万");
                }
            } else {
                listener.OnFinish(UserInfo.TYPE_CHECKRESULT_FAIL, accountInfo);
                Log4an.e(logTag, "error userGrade grade=" + accountInfo.getGrade());
            }
        } else {
            listener.OnFinish(UserInfo.TYPE_CHECKRESULT_FAIL_RECHECK, accountInfo);
        }
    }

    @Override
    public List<BasicNameValuePair> setParams() {
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        String key = "c56cf32e9a52a265ae47cd50570266cc";
        long timestamp = System.currentTimeMillis();
        String strTimestamp = timestamp + "";
        String md5_key_sb = key + timestamp;
        String md5_key = Util.MD5(md5_key_sb.toString());
        params.add(new BasicNameValuePair("key", md5_key));// md5
        params.add(new BasicNameValuePair("timestamp", strTimestamp));// 时间戳
        return params;
    }

    @Override
    public String getRequestUrl() {
        return Urls.getGetAccountDetailUrl();
    }

    public interface OnCheckUserStateOnlineListener {
        /**
         * 在线获取用户信息
         * 
         * @param type：会员成功，准会员成功，准会员成功分配新用户，失败需重新验证，失败，网络失败
         * @param info
         */
        public void OnFinish(int type, AccountInfo info);
    }
}
